import router from "./router"
import { usePermissionStore } from '@/stores/modules/permission'
import { getToken } from '@/utils/auth'
const whiteList = ['/login']

router.beforeEach((to, from, next) => {
    const permissionStore = usePermissionStore()
    const hastoken = getToken()   // 登录认证
    // NProgress.start();
    console.log(to)
    console.log(hastoken)
    if (hastoken) {
        if (to.path === '/login') {
            next({ path: '/' })
            NProgress.done()
        }else{
            if (permissionStore.hasinit) {  // 已经根据角色生成了动态路由
                next()
            } else {
                permissionStore.generateRoutes().then(accessRoutes => {
                    next({ ...to, replace: true });
                })
            }
        }
        
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            // alert(2)
            console.log(666)
            next(`/login`) 
        }
    }
})